package com.xinke.edu.mypetadoption.fragment2.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.viewholder.QuickViewHolder;
import com.xinke.edu.mypetadoption.R;
import com.xinke.edu.mypetadoption.entity.Product;
import com.xinke.edu.mypetadoption.entity.ResultBody;
import com.xinke.edu.mypetadoption.util.RetrofitUtil;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class SpAdpter extends BaseQuickAdapter<Product, QuickViewHolder> {
    @Override
    protected void onBindViewHolder(@NonNull QuickViewHolder quickViewHolder, int i, @Nullable Product product) {
        if (product == null) return;

        ImageView img = quickViewHolder.getView(R.id.sptp);
        Glide.with(getContext()).load(product.getCoverImage()).into(img);
        quickViewHolder.setText(R.id.spbt, product.getName());
        quickViewHolder.setText(R.id.spjg, "￥" + product.getPrice());
        quickViewHolder.setText(R.id.spkc, "库存：" + product.getStock());
        
        // 删除按钮点击事件
        quickViewHolder.getView(R.id.dd_sc).setOnClickListener(v -> {
            showDeleteConfirmDialog(getContext(), product, i);
        });
        
        // 编辑按钮点击事件（暂时只显示Toast）
        quickViewHolder.getView(R.id.dd_bj).setOnClickListener(v -> {
            Toast.makeText(getContext(), "编辑商品：" + product.getName(), Toast.LENGTH_SHORT).show();
        });
    }
    
    /**
     * 显示删除确认对话框
     */
    private void showDeleteConfirmDialog(Context context, Product product, int position) {
        new AlertDialog.Builder(context)
                .setTitle("删除确认")
                .setMessage("确定要删除商品\"" + product.getName() + "\"吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteProduct(product.getId(), position);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
    
    /**
     * 删除商品
     */
    private void deleteProduct(Long productId, int position) {
        RetrofitUtil.getApi().deleteProduct(productId.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {}

                    @Override
                    public void onNext(@NonNull ResultBody result) {
                        if (result.getCode() == 200) {
                            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                            // 从列表中移除该商品
                            removeAt(position);
                        } else {
                            Toast.makeText(getContext(), "删除失败：" + result.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(getContext(), "删除失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {}
                });
    }

    @NonNull
    @Override
    protected QuickViewHolder onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new QuickViewHolder(R.layout.item_sj_sp,viewGroup);
    }
}
